﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Query;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.SdkTypeProxy.Metadata;
using Microsoft.Crm.Sdk.Metadata;

using Custom.Crm.General.Utility;

namespace Custom.Crm.General
{
    public class RandomCharacters : IPlugin
    {
        /// <summary>The length of the random text.</summary>
        private int m_Length = 6;

        /// <summary>The field to be randomly generated.</summary>
        private string m_Field = null;

        /// <summary>The characters available for random selection.</summary>
        private string m_CharacterPool = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        /// <summary>
        /// Constructor for the class - recieves two variables when the workflow engine.
        /// </summary>
        /// <param name="unSecure">Unsecure data entered while registering the plugin.
        /// Example of unSecure configuration:  <Configuration><Length>6</Length><Field>mailer_ref_number</Field><CharacterPool>0123456789</CharacterPool></Configuration>
        /// </param>
        /// <param name="Secure">secure data entered while registering the plugin</param>
        public RandomCharacters(string unSecure, string Secure)
        {
            if (unSecure != null && unSecure.Length > 0)
            {
                // Get the Configurations from the unSecure properties.
                Configuration m_config = new Configuration(unSecure);

                // The config item that holds the field name.
                string Field = m_config["Field"];
                if (Field != null) m_Field = Field;

                // The config item that holds the character pool.
                string CharacterPool = m_config["CharacterPool"];
                if (CharacterPool != null) m_CharacterPool = CharacterPool;

                // The config item that holds the random field length to be generated.
                string Length = m_config["Length"];
                if (Length != null)
                    int.TryParse(Length, out m_Length);
            }
        }

        public void Execute(IPluginExecutionContext context)
        {
            if (m_Field != null)
            {
                DynamicEntity entity = (DynamicEntity)context.InputParameters.Properties["Target"];

                if (entity.Properties.Contains(m_Field) == false)
                    entity.Properties.Add(new StringProperty(m_Field, null));

                entity.Properties[m_Field] = GetRandomString(m_Length);
            }
        }

        /// <summary>
        /// Generates a random string.
        /// </summary>
        /// <param name="rnd">A random object</param>
        /// <param name="length">The length of the random string you want generated.</param>
        /// <returns>A random string</returns>
        public string GetRandomString(int length)
        {
            Random rnd = new Random();
            StringBuilder rs = new StringBuilder();

            while (length-- != 0)
            {
                double rndNo = rnd.NextDouble();
                int index = Convert.ToInt32(rndNo * m_CharacterPool.Length);
                rs.Append(m_CharacterPool[index]);
            }

            return rs.ToString();
        }
    }
}
